/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tp_imag.gui.area.selection;
import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import ij.gui.*;
import ij.io.FileSaver;

import java.awt.Canvas;
import java.awt.event.*;
import tp_imag.model.ActualImage;

/** Mouse_Listener
  *
  * Plugin for listening to mouse clicks in image window.
  *
  * This is an example plugin from the ImageJ plugin writing tutorial.
  * The tutorial can be downloaded at
  * http://www.fh-hagenberg.at/mtd/depot/imaging/imagej
  */

/**
 *
 * @author LRinaudo
 */
public class Mouse_Listener implements PlugInFilter, MouseListener {
	
    //private static double PORCENTAGEX = 8;
    //private static double PORCENTAGEY = 8;
    private static double PORCENTAGE = 8;

    private static String NEW_IMAGE_PATH = "areaSelection.jpg";
	ImagePlus img;
    ImagePlus newImage;
    ImageCanvas canvas;
    javax.swing.JPanel jPanel;
    int xInitial;
    int yInitial;
    

    public int setup(String arg, ImagePlus img) {
        this.img = img;
        return DOES_ALL;
    }

    public int setup(String arg, ImagePlus img, javax.swing.JPanel jPanel) {
        this.img = img;
        this.jPanel = jPanel;
        //PORCENTAGEX = this.img.getWidth()/jPanel.getWidth();
        //PORCENTAGEY = this.img.getHeight()/jPanel.getHeight();
        return DOES_ALL+NO_CHANGES+ROI_REQUIRED;
    }

    public void run(ImageProcessor ip) {
    	this.canvas = new ImageCanvas(this.img);
    	this.canvas.addMouseListener(this);
    	this.jPanel.addMouseListener(this);
    }

	// method is called when mouse is clicked
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        int offscreenX = canvas.offScreenX(x);
        int offscreenY = canvas.offScreenY(y);
        IJ.showMessage("mousePressed: "+offscreenX+","+offscreenY);
    }

    public void mousePressed(MouseEvent e) {
    	this.xInitial = e.getX();
    	this.yInitial = e.getY();       
    }

    public void mouseReleased(MouseEvent e) {
    	int xFinal = e.getX();
    	int yFinal = e.getY();
    	
    	//this.img.setRoi((int)(this.xInitial* PORCENTAGEX) ,(int)( this.yInitial * PORCENTAGEY), (int)((xFinal - this.xInitial)* PORCENTAGEX), (int) ((yFinal - this.yInitial) * PORCENTAGEY));
        this.img.setRoi((int)(this.xInitial* PORCENTAGE) ,(int)( this.yInitial * PORCENTAGE), (int)((xFinal - this.xInitial)* PORCENTAGE), (int) ((yFinal - this.yInitial) * PORCENTAGE));
    	
    	ImagePlus imageAux = this.img;
    	this.newImage = new ImagePlus();
    	this.newImage.setProcessor(imageAux.getProcessor().crop());
    	   	
    	this.img.show();
    	this.img.updateAndDraw();
    	
    	this.newImage.show();
    	this.newImage.updateAndDraw();
    	//this.newImage.getProcessor().copyBits(this.img.getProcessor(), (int)(this.xInitial* PORCENTAGE), (int)( this.yInitial * PORCENTAGE), Blitter.OR);
    	FileSaver fileSaver = new FileSaver(this.newImage);
        ActualImage.getInstance().setOriginal(this.newImage.getBufferedImage());
    	
    	fileSaver.saveAsJpeg(NEW_IMAGE_PATH);
    }    	
    	
    	
    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public ImagePlus getImage(){
    	return this.newImage;
    }
}
